// gulp.task   定义任务
// gulp.src    获取文件流
// gulp.pipe   处理文件流
// gulp.dest   输出文件流
// gulp.watch  监听文件
// gulp.start  执行某个任务


var gulp = require("gulp");
var less = require("gulp-less");
var autoprefixer = require("gulp-autoprefixer");
var rename = require("gulp-rename");
var cleancss = require("gulp-clean-css");
var concat = require("gulp-concat");
var bs = require("browser-sync");
var sourcemap = require('gulp-sourcemap');

gulp.task("serve", function(){
    bs.init({
        server: {
            baseDir: "./" //服务主目录
        },
        startPath: "src/index.html" //服务启动主文件
    });
    
    gulp.watch("./{html,js}/*.{html,js}", bs.reload);
})

gulp.task("lessc", function(){
  gulp.src("src/less/*.less")
      .pipe( less() )
      .pipe( autoprefixer({browsers: ['last 2 versions']}) )
      .pipe( gulp.dest("dist/css/") )
      .pipe( cleancss() )
      .pipe( rename({suffix: ".min"}) )
      .pipe( gulp.dest("src/css/") )
      .pipe( bs.reload({stream:true}) );
})

gulp.task("css-concat", function(){
    gulp.src("css/*.min.css")
        .pipe( concat("all.css") )
        .pipe( gulp.dest("min/") );
})

gulp.task("watch", function(){
    gulp.watch("src/less/*.less", ["lessc"]);
    gulp.watch('src/*.html',function(){
      bs.reload();
    })
})


gulp.task("default", ['serve','watch'])